<?php
/**
 * 项目：红河移动电路资源管理信息系统
 *
 * 公司：红河移动分公司
 *
 * @author 李朝应
 *
 * E-mail:lichaoying.hh@163.com
 *
 */
include("conf.inc.php");
class Admin extends CI_Controller {
       /*
     * 构造函数
     */
     
  function __construct()
  {
       parent::__construct();
        $this->load->model('admin_model');
        $this->load->model('admin_event_model');
        $this->load->model('org1_model');
        $this->load->model('org2_model');
        $this->load->model('org3_model');
        $this->load->model('level_model');
        $this->load->model('user_model','Muser');
   }
    /*
     * 系统过程
     */
     
    function parm_1(){
        $conf = new conf(); 
        $data['title'] =$conf->title; 
        $data['version'] =$conf->version." For ".$conf->System_OS;
        $data['copyright']=$conf->copyright;
        $this->load->vars($data);
    }
     function SQLsafe( $string ) {   //阻止SQL注入式攻击,只适用PHP5
        return "'" . mysql_real_escape_string( $string ) . "'";
    }
     function SQLsafe2( $string ) {   //阻止SQL注入式攻击,只适用PHP5
        return  mysql_real_escape_string( $string );
    }
   function isLogin(){
    $user=$this->session->userdata('crms_admin_auth');
    $pwd=$this->session->userdata('crms_pwd_auth');
    if($this->admin_model->isAdmin($user,$pwd)){
        return TRUE;
    }else{
      return FALSE;
  }
  }
  function ifLogin(){
     if(!$this->isLogin()){
         redirect("admin/login");
     }//判断登录状态
  }
   function code_check($str){
		if($str==$this->session->userdata("crms_captcha")){
            $this->session->unset_userdata('crms_captcha');//释放验证码
			return TRUE;
		}else{
			return FALSE;
		}
	}
    function savePwd(){
        //取表单数据
        $this->ifLogin();//后台登录认证
        $error="";
        $admin="admin"; //超级用户名登录名，一般不变。
        $name=$this->security->xss_clean($this->input->post('name'));
        $email=$this->security->xss_clean($this->input->post('email'));
        $tel=$this->security->xss_clean($this->input->post('tel'));
        $old=$this->security->xss_clean($this->input->post('old'));
        $password=$this->security->xss_clean($this->input->post('password'));
       if($this->admin_model->isAdmin($admin,$old)){
                  $this->admin_model->saveAdmin($admin,$name,$email,$tel,$password);
                  $session_data = array('crms_admin_auth'=>$admin,'crms_pwd_auth'=>$password); //重新更新写入到Session
	              $this->session->set_userdata($session_data);
                  $this->admin_event_model->setEvent("修改管理员信息成功");
                  $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料保存成功!</span>";
               
         }else{
              $this->admin_event_model->setEvent("修改管理员信息出错");
              $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 出错，原始密码不匹配!</span>";
       }
         echo $error;
        
    }
    /*
     * 系统页面
     */
    function index() {
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $this->load->view('admin/index');
    }
    function top(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $this->load->view('admin/top');
    }
    function menu(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $this->load->view('admin/menu');
    }
    function info(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $this->load->view('admin/info');
    }
    function editpasswd(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $admin['admin'] = $this->admin_model->getAdmin("admin");
        $this->load->view('admin/editpasswd', $admin);
    }
    function captcha(){
		$this->load->library('CaptchaImage');
		ob_start();
        $session_data = array('crms_captcha'=>$this->captchaimage->getString()); //写入到Session
	    $this->session->set_userdata($session_data);
		$this->captchaimage->setFont('./system/fonts/type-ra.ttf',16);
		$this->captchaimage->draw();
		ob_flush();
	}
    function login(){
      if($this->isLogin()){
           redirect("admin/index");
       }
        $this->parm_1();
        $msg['error'] = "";
        $this->load->view("admin/login",$msg);
    }

   function checklogin(){
       if($this->isLogin()){
           redirect("admin/index");
       }
       $error="";
       //取得表单数量
       $user=$this->security->xss_clean($this->input->post("username"));
       $pwd=$this->security->xss_clean($this->input->post("password"));
       $code=$this->security->xss_clean($this->input->post("chk"));
       if($this->code_check($code)){
          $error="正在载入. . .";
          if($this->admin_model->isAdmin($user,$pwd)){
              $session_data = array('crms_admin_auth'=>$user,'crms_pwd_auth'=>$pwd); //写入到Session
	          $this->session->set_userdata($session_data);
              $this->admin_event_model->setEvent("管理员登录成功");
              redirect("admin/index");
          }else{
              $this->admin_event_model->setEvent("管理员登录密码错误");
              $error="对不起，用户名或者密码错误,请重新输入!";
          }

       }else{
          $this->admin_event_model->setEvent("管理员登录验证码错误");
          $error="对不起，验证码错误,请重新输入!";
       }
        $this->parm_1();
        $msg['error'] = $error;
        $this->load->view("admin/login",$msg);
   }
   function org1_all(){
          $this->ifLogin();//后台登录认证
          $this->parm_1();
          if(!($this->org1_model->getCount()==0)) {
            $org1['org1'] = $this->org1_model->getOrg1ALL();
            $this->load->view('admin/org1_all',$org1);
         }else{
            $this->load->view('admin/org1_all');
         }
   }
   function org1_add(){
          $this->ifLogin();//后台登录认证
          $this->parm_1();
          if(!($this->org1_model->getCount()==0)) {
             $org1['org1'] = $this->org1_model->getOrg1ALL();
            $this->load->view('admin/org1_add',$org1);
         }else{
            $this->load->view('admin/org1_add');
         }
   }
   function org1_del($id,$rd_url){
       $this->ifLogin();//后台登录认证
       settype($id,"integer");//设为整型，防SQL注入
       $this->org1_model->delOrg1($id);
       redirect("admin/".$rd_url);
   }
   function org1_edit($org1_id){
         $this->ifLogin();//后台登录认证
          $this->parm_1();
          $orgdata['org1'] = $this->org1_model->getOrg1($org1_id);
          $this->load->view('admin/org1_edit',$orgdata);
   }
    function org2_del($id,$rd_url){
      $this->ifLogin();//后台登录认证
      settype($id,"integer");//设为整型，防SQL注入
      $this->org2_model->delOrg2($id);
       redirect("admin/".$rd_url);
   }
   function org2_edit($org2_id){
         $this->ifLogin();//后台登录认证
          $this->parm_1();
          $orgdata['org1'] = $this->org1_model->getOrg1ALL();
          $orgdata['org2'] = $this->org2_model->getOrg2($org2_id);
          $this->load->view('admin/org2_edit',$orgdata);
   }
    function saveOrg1(){
       //取表单数据
        $this->ifLogin();//后台登录认证
        $error="";
        $name=trim($this->input->post('name'));
        $fzr=trim($this->input->post('fzr'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
        $word=trim($this->input->post('word'));
        if(!$this->org1_model->hasOrg1($name) ){
          $this->org1_model->setOrg1($name,$fzr,$email,$tel,$word);
          $this->admin_event_model->setEvent("添加了一级组织成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料保存成功!</span>";
       }else{
           $error="<img src=\"images/error.gif.gif\" /><span class=\"about_content_title\"> 资料己经保存成功，无需要重新提交!</span>";
       }
       echo $error;
   }
   function updateOrg1($id){
        $this->ifLogin();//后台登录认证
       //取表单数据
        $error="";
        $name=trim($this->input->post('name'));
        $fzr=trim($this->input->post('fzr'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
        $word=trim($this->input->post('word'));
        if(!$this->org1_model->hasSameRec($id,$name) ){
           $this->org1_model->updateOrg1($id,$name,$fzr,$email,$tel,$word);
           $this->admin_event_model->setEvent("更新了一级组织成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料更新保存成功!</span>";
        }else{
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料更新失败，己经存在相同名称的记录!</span>";
        }
       echo $error;
   }
      function org2_all(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $org1['org2'] = $this->org2_model->getOrg2ALL();
        $this->load->view('admin/org2_all',$org1);
    }
    function org2_add(){
          $this->ifLogin();//后台登录认证
          $this->parm_1();
         if($this->org1_model->getCount()==0&&$this->org2_model->getCount()==0) {
            $this->load->view('admin/org2_add');
         }else{
              $org1['org1'] = $this->org1_model->getOrg1ALL();
              $org1['org2'] = $this->org2_model->getOrg2ALL();
             $this->load->view('admin/org2_add',$org1);
         }
   }
    function saveOrg2(){
      $this->ifLogin();//后台登录认证
       //取表单数据
        $error="";
        $org1_id=trim($this->input->post('org1_name'));
        $name=trim($this->input->post('name'));
        $fzr=trim($this->input->post('fzr'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
       if(!$this->org2_model->hasOrg2($org1_id,$name) ){
           $this->org2_model->setOrg2($org1_id,$name,$fzr,$email,$tel);
           $this->admin_event_model->setEvent("添加了二级组织成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料保存成功!</span>";
       }else{
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料己经保存成功，无需要重新提交!</span>";
       }
       echo $error;
   }
  function updateOrg2($id){
       $this->ifLogin();//后台登录认证
       //取表单数据
        $error="";
        $org1_id=trim($this->input->post('org1_name'));
        $name=trim($this->input->post('name'));
        $fzr=trim($this->input->post('fzr'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
        if(!$this->org2_model->hasSameRec($id,$org1_id,$name) ){
            $this->org2_model->updateOrg2($id,$org1_id,$name,$fzr,$email,$tel);
            $this->admin_event_model->setEvent("更新了二级组织成功");
            $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料更新保存成功!</span>";
        }else{
            $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料更新失败，己经有相同名称的记录!</span>";
        }
       echo $error;
   }
    function org3_add(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
         if($this->org1_model->getCount()==0&&$this->org2_model->getCount()==0) {
            $this->load->view('admin/org2_add');
         }else{
              $orgdata['org1'] = $this->org1_model->getOrg1ALL();
             // $org1['org2'] = $this->org2_model->getOrg2ALL();
             $this->load->view('admin/org3_add',$orgdata);

         }
     
    }
    function org3_del($id,$rd_url){
     $this->ifLogin();//后台登录认证
     settype($id,"integer");//设为整型，防SQL注入
     $this->org3_model->delOrg3($id);
     redirect("admin/".$rd_url);
   }
   function org3_del2($id){
     $this->ifLogin();//后台登录认证
     settype($id,"integer");//设为整型，防SQL注入
     $this->org3_model->delOrg3($id);
     echo "ok";
   }
    function org3_edit($org3_id){
          $this->ifLogin();//后台登录认证
          $this->parm_1();
         if($this->org1_model->getCount()==0&&$this->org2_model->getCount()==0) {
            $this->load->view('admin/org2_add');
         }else{
              $org1['org1'] = $this->org1_model->getOrg1ALL();
              $org1['org2'] = $this->org2_model->getOrg2ALL();
              $org1['org3'] = $this->org3_model->getOrg3($org3_id);
             $this->load->view('admin/org3_edit',$org1);

         }
    }
     function saveOrg3(){
        $this->ifLogin();//后台登录认证
       //取表单数据
        $error="";
        $org2_id=trim($this->input->post('org2_name'));
        $name=trim($this->input->post('name'));
        $fzr=trim($this->input->post('fzr'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
       if(!$this->org3_model->hasOrg3($org2_id,$name) ){
           $this->org3_model->setOrg3($org2_id,$name,$fzr,$email,$tel);
           $this->admin_event_model->setEvent("添加了三级组织成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料保存成功!</span>";
       }else{
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料己经保存成功，无需要重新提交!</span>";
       }
       echo $error;
   }
    function updateOrg3($id){
       $this->ifLogin();//后台登录认证
       //取表单数据
        $error="";
        $org2_id=trim($this->input->post('org2_name'));
        $name=trim($this->input->post('name'));
        $fzr=trim($this->input->post('fzr'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
        if(!$this->org3_model->hasSameRec($id,$org2_id,$name) ){
           $this->org3_model->updateOrg3($id,$org2_id,$name,$fzr,$email,$tel);
           $this->admin_event_model->setEvent("更新了三级组织成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料更新保存成功!</span>";
        }else{
          $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料更新失败，己经有相同名称的记录!</span>";
        }
       echo $error;
   }
  function org3_all(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $orgdata['org3'] = $this->org3_model->getOrg3ALL();
        $this->load->view('admin/org3_all',$orgdata);
    }
  function org3_find(){
    $this->ifLogin();//后台登录认证
     $name=$this->SQLsafe2(trim($this->input->post('name')));
     $lastname=trim($this->input->post('lastname'));
    if($name==""):
     $this->parm_1();
     $this->load->view('admin/org3_find');
    else:
     $this->parm_1();
     $orgdata['org3'] = $this->org3_model->getOrg3By($name);
     $orgdata["lastname"]=$name;
     $this->load->view('admin/org3_find',$orgdata);
    endif;
  }
   function findOrg2($org1_id){
    $this->ifLogin();//后台登录认证
    echo $this->org2_model->getOrg2In($org1_id);//返回一个Option列表
   
   }
   function findOrg3($org2_id){
    $this->ifLogin();//后台登录认证
    echo $this->org3_model->getOrg3In($org2_id);//返回一个Option列表

   }
   /*
    * 权根据管理
    */
   function lev_add(){
          $this->ifLogin();//后台登录认证
          $this->parm_1();
          if(!($this->level_model->getCount()==0)) {
             $datas['lev'] = $this->level_model->getALL();
             $this->load->view('admin/lev_add',$datas);
         }else{
             $this->load->view('admin/lev_add');
         }
   }
   function saveLev(){
       $this->ifLogin();//后台登录认证
      //取表单数据
        $error="";
        $name=trim($this->input->post('name'));
        $dspt=trim($this->input->post('dspt'));
        $lev=trim($this->input->post('lev'));
        if(!$this->level_model->hasSameLev($name,$lev) ){
          $this->level_model->setLev($name,$dspt,$lev);
          $this->admin_event_model->setEvent("添加权根字串成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料保存成功!</span>";
       }else{
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 己经有相同级别的记录保存成功，无需要重新提交!</span>";
       }
       echo $error;
   }
   function lev_edit($id){
          $this->ifLogin();//后台登录认证
          $this->parm_1();
          $levdata['lev'] = $this->level_model->getLev($id);
          $this->load->view('admin/lev_edit',$levdata);
   }
   function updateLev($id){
        $this->ifLogin();//后台登录认证
        $error="";
        $name=trim($this->input->post('name'));
        $dspt=trim($this->input->post('dspt'));
        $lev=trim($this->input->post('lev'));
         if(!$this->level_model->hasSameRec($id,$name,$lev) ){
            $this->level_model->updateLev($id,$name,$dspt,$lev);
            $this->admin_event_model->setEvent("更新了权限字串成功");
            $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料更新保存成功!</span>";
        }else{
            $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料更新失败，己经有相同名称的记录!</span>";
        }
       echo $error;
   }
    function lev_del($id,$rd_url){
      $this->ifLogin();//后台登录认证
      settype($id,"integer");//设为整型，防SQL注入
      $this->level_model->delLev($id);
       redirect("admin/".$rd_url);
   }
   function user_add(){
      $this->ifLogin();//后台登录认证
      $this->parm_1();
      $orgdata['levlist']= $this->level_model->getLevOpt();
      $orgdata['org1'] = $this->org1_model->getOrg1ALL();
      $this->load->view('admin/user_add',$orgdata);
   }
   function userNameAuth(){
     $this->ifLogin();//后台登录认证
     $error="";
     $name=trim($this->input->post('name'));
     if($name!=""){
       if($this->Muser->getUserNameAuth($name)){
        $error="<img src=\"images/check_error.gif\" /><span class=\"about_content_title\"> 己经存在相同'姓名'的记录!</span>";
       }else{
        $error="<img src=\"images/check_right.gif\" /><span class=\"about_content_title\"> </span>";
      }
     }
     echo $error;
   }
  function LoginNameAuth(){
     $this->ifLogin();//后台登录认证
     $error="";
     $name=trim($this->input->post('name'));
   if($name!=""){
     if($this->Muser->getLoginNameAuth($name)){
        $error="<img src=\"images/check_error.gif\" /><span class=\"about_content_title\"> 己经有相同'登录名称'的记录!</span>";
     }else{
        $error="<img src=\"images/check_right.gif\" /><span class=\"about_content_title\"> </span>";
     }
   }
     echo $error;
   }
   function saveUser(){
        $this->ifLogin();//后台登录认证
        $error="";
        $org3_id=trim($this->input->post('org3_name'));
        $name=trim($this->input->post('name'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
        $login=trim($this->input->post('login'));
        $pw=trim($this->input->post('pw'));
        $lev=trim($this->input->post('lev'));
       if($this->Muser->hasUserName($org3_id,$name) ):
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\">资料提交失败,在同一个三级组织下姓名重复!</span>";
       elseif($this->Muser->hasUserLogin($login)):
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料提交失败，己经存在相同登录名称的用户!</span>";
       else:
         $this->Muser->setUser($org3_id,$name,$email,$tel,$login,$pw,$lev);
         $this->admin_event_model->setEvent("添加了用户成功");
         $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 资料保存成功!</span>";
       endif;//流程控制的替代语法
       echo $error;
   }
  function user_all(){
        $this->ifLogin();//后台登录认证
        $this->parm_1();
        $orgdata['user'] = $this->Muser->getUserALL();
        $this->load->view('admin/user_all',$orgdata);
    }
 function user_edit($id){
     $this->ifLogin();//后台登录认证
     $this->parm_1();
     $datas['levlist']= $this->level_model->getLevUpdateOpt($id);
     $datas['org1'] = $this->org1_model->getOrg1ALL();
     $datas['org3'] = $this->org3_model->getOrg3All();
     $datas['user'] = $this->Muser->getUserBy($id);
     $this->load->view('admin/user_edit',$datas);
       }
 function userNameUpdateAuth($id){
     $this->ifLogin();//后台登录认证
     $error="";
     $name=trim($this->input->post('name'));
     if($name!=""){
       if($this->Muser->getUserNameUpdateAuth($id,$name)){
        $error="<img src=\"images/check_error.gif\" /><span class=\"about_content_title\"> 其它三级组织中己经存在相同'姓名'的记录!</span>";
       }else{
        $error="<img src=\"images/check_right.gif\" /><span class=\"about_content_title\"> </span>";
      }
     }
     echo $error;
   }
  function LoginNameUpdateAuth($id){
     $this->ifLogin();//后台登录认证
     $error="";
     $name=trim($this->input->post('name'));
   if($name!=""){
     if($this->Muser->getLoginNameUpdateAuth($id,$name)){
        $error="<img src=\"images/check_error.gif\" /><span class=\"about_content_title\"> 其它三级组织中己经有相同'登录名称'的记录!</span>";
     }else{
        $error="<img src=\"images/check_right.gif\" /><span class=\"about_content_title\"> </span>";
     }
   }
     echo $error;
   }
 function updateUser($id){
        $this->ifLogin();//后台登录认证
        $error="";
        $org3_id=trim($this->input->post('org3_name'));
        $name=trim($this->input->post('name'));
        $email=trim($this->input->post('email'));
        $tel=trim($this->input->post('tel'));
        $login=trim($this->input->post('login'));
        $pw=trim($this->input->post('pw'));
        $lev=trim($this->input->post('lev'));
        if($this->Muser->getLoginNameUpdateAuth($id,$login)):
           $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 资料更新失败，己经有相同登录名称的记录!</span>";
        else:
           $this->Muser->updateUser($id,$org3_id,$name,$email,$tel,$login,$pw,$lev);
           $this->admin_event_model->setEvent("更新了用户信息成功");
           $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 更新用户资料成功!</span>";
        endif;
        echo $error;
 }
 function user_del($id,$rd_url){
    $this->ifLogin();//后台登录认证
    settype($id,"integer");//设为整型，防SQL注入
    $this->Muser->delUser($id);
    redirect("admin/".$rd_url);
 }
 function user_del2($id){
    $this->ifLogin();//后台登录认证
    settype($id,"integer");//设为整型，防SQL注入
    $this->Muser->delUser($id);
    echo "ok";
 }
function user_find(){
    $this->ifLogin();//后台登录认证
     $name=$this->SQLsafe2(trim($this->input->post('name')));
     $lastname=trim($this->input->post('lastname'));
    if($name==""):
     $this->parm_1();
     $this->load->view('admin/user_find');
    else:
     $this->parm_1();
     $orgdata['user'] = $this->Muser->getUserByName($name);
     $orgdata["lastname"]=$name;
     $this->load->view('admin/user_find',$orgdata);
    endif;
  }
function admin_event($id=0){
    $this->ifLogin();//后台登录认证
    $this->parm_1();
    settype($id,"integer"); //设$id为整型
    $this->load->library('pagination');
    //自定义分页
    $config['first_link'] = '第一页';
    $config['last_link'] = '最后一页';
    $config['next_link'] = '下一页';
    $config['prev_link'] = '上一页';
    $config['cur_tag_open'] = '<b>&nbsp;';
    $config['cur_tag_close'] = '&nbsp;</b>';
    $config['full_tag_open'] = '页数：';
    $config['base_url'] = site_url("admin/admin_event");
    $config['total_rows'] = $this->admin_event_model->getAdminEventTotal();
    $config['per_page'] = '30';
    //初始化分页参数
    $this->pagination->initialize($config);
    $datas['pages']=$this->pagination->create_links();
    $datas['admin_event'] = $this->admin_event_model-> getAdminEvent($id,30);
    $this->load->view('admin/admin_event',$datas);
   }
   function logout(){
        $this->admin_event_model->setEvent("管理员登录退出系统");
		$this->session->unset_userdata('crms_admin_auth');
        $this->session->unset_userdata('crms_pwd_auth');
		redirect('admin/login');
	}
}

?>